import 'package:flutter/services.dart';

class SerialPortManager {
  static const _channel = MethodChannel('com.example.serial_port');

  /// 打开串口
  static Future<String> openPort({
    required String portName,
    required int baudRate,
  }) async {
    try {
      final result = await _channel.invokeMethod('openPort', {
        'portName': portName,
        'baudRate': baudRate,
      });
      return result as String;
    } on PlatformException catch (e) {
      throw Exception('Failed to open port: ${e.message}');
    }
  }

  /// 关闭串口
  static Future<String> closePort() async {
    try {
      final result = await _channel.invokeMethod('closePort');
      return result as String;
    } on PlatformException catch (e) {
      throw Exception('Failed to close port: ${e.message}');
    }
  }

  /// 发送数据到串口
  static Future<String> sendData(String data) async {
    try {
      final result = await _channel.invokeMethod('sendData', {
        'data': data,
      });
      return result as String;
    } on PlatformException catch (e) {
      throw Exception('Failed to send data: ${e.message}');
    }
  }
}